package com.aispeech.task.api.config;

import com.aispeech.task.api.handler.TaskHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class Routers implements WebFluxConfigurer {
    @Bean
    public RouterFunction<?> healthz() {
        return route()
                .GET("/healthz", req -> ServerResponse.ok().bodyValue("OK"))
                .build();
    }

    @Bean
    public RouterFunction<ServerResponse> task(TaskHandler taskHandler) {
        return route()
                .path("/task",
                        taskBuilder -> taskBuilder.nest(
                                accept(MediaType.APPLICATION_FORM_URLENCODED),
                                build -> build
                                        .POST("/create", taskHandler::create)
                                        .POST("/progress", taskHandler::progress)
                                        .POST("/result", taskHandler::result)
                        ))
                .build();
    }
}
